package data.dep;

import java.net.MalformedURLException;
import java.util.ArrayList;

import data.crawling.Builder;

import websphinx.Crawler;
import websphinx.Element;
import websphinx.Link;
import websphinx.Page;
import websphinx.Tag;

/**
 * @deprecated
 * @author j55wu
 *
 */
public 	class PriceCrawler extends Crawler{
	//a list of price, vendor name, vendor url, and vendor rating by users
	public ArrayList<String[]> vendors;
	
	public PriceCrawler(){
		this.vendors = new ArrayList<String[]>();
	}
	
	protected String printVendors(ArrayList<String[]> vs){
		String s = "";
		for(String[] v:vs){
			s += "\n"+v[0]+", "+v[1]+", "+v[3];
		}
		return s;
	}
	
//	@Override
//	public boolean shouldVisit(Link l){
//		Page p = l.getPage();
//		String title = p.getTitle().toLowerCase();
//		if(title.contains("battery") 
//				|| title.contains("batteries")
//				|| title.contains("charger")
//				|| title.contains("accessories")
//				|| title.contains("accessory")){
//			return false;
//		}
//		return true;
//	}
	
	private ArrayList<String[]> getResultTable(Element res0){
		//System.out.println(res0);
		String vurl = null;
		for(Element e = res0.getChild(); e!= null; e = e.getSibling()){
			if(e.getHTMLAttribute("class").contains("result-title-description")){
				vurl = "http://www.google.com" + e.getChild().getChild().getHTMLAttribute("href").trim();
				break;								
			}
		}
		ResultTableCrawler rtc = new ResultTableCrawler();
		try {
			rtc.setRoot(new Link(vurl));
			rtc.setMaxDepth(0);
			rtc.setDepthFirst(true);
			rtc.setDomain(Crawler.SUBTREE);
			rtc.setLinkType(Crawler.HYPERLINKS);
			rtc.clear();
			rtc.run();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	//	System.out.println("pc: "+this.printVendors(this.vendors));
	//	System.out.println("rtc: "+this.printVendors(rtc.vendors));
		return rtc.vendors;
	}
	
	private boolean invalidResult(Element res){
		Element it = res.getChild().getChild().getChild().getChild();
		if(it != null){
			String tt = it.getHTMLAttribute("title").toLowerCase();
			if(tt.contains("battery") || tt.contains("batteries")
					|| tt .contains("accessories") || tt.contains("accessory")
					|| tt.contains("charger")
					|| tt.contains("cable")
					|| tt.contains("adapter")){
				//System.out.println("Not visited: "+tt);
				return true;
			}
		}
		return false;
	}
	
	@Override
	public void visit(Page page){
		//get the url of all online vendors and prices
		//System.out.println("tested");
		for(Tag t:page.getTags()){
			if(t.getTagName().compareToIgnoreCase(Tag.OL) == 0 && t.getHTMLAttribute("id") != null){
				//get the first item <li>, i.e., res0, then loop.
				for(Element res = t.getElement().getChild();res != null; res = res.getNext()){
					//System.out.println("res: "+res.getHTMLAttribute("id"));
					boolean complicated = false;
					if(this.invalidResult(res)){
						continue;
					}
					String[] v = new String[4];
					Element cont = res.getChild();
					for(Element el = cont.getChild(); el != null; el = el.getSibling()){
						//each loop gets one v for every result
						if(el.getHTMLAttribute("class").contains("result-price-seller")){
							//get subcomponents of v
							for(Element sub=el.getChild(); sub != null; sub=sub.getSibling()){	
								if(sub.getHTMLAttribute("class").contains("result-button")){
									//decide if a separate crawl is necessary
									complicated =true;
									break;
								}
								//else parse this page alone to get first few vendor information
								if(sub.getHTMLAttribute("class").contains("result-price")){
									String price =sub.toText().trim();
									price = price.substring(price.indexOf('$')+1, price.indexOf(' '));
									v[0]=price;
								}
								if(sub.getHTMLAttribute("class").compareToIgnoreCase("result-seller") == 0){
									String vname = sub.toText().trim();
									v[1] = vname;
								}
								if(sub.getHTMLAttribute("class").compareToIgnoreCase("result-seller-rating") == 0){
									//some vendors have ratings. but not shown in the page!
									Element span = sub.getChild().getChild();
									if(span != null){
										String vrate = span.getHTMLAttribute("title").toString().trim();
										if(vrate.indexOf(' ') >= 0){
											//e.g., 4 stars => 4
											vrate = vrate.substring(0,  vrate.indexOf(' '));
										}
										v[3] = vrate;
									}										
								}									
							}
							
						}else
						if(el.getHTMLAttribute("class").contains("result-title-description")){
							String vurl = el.getChild().getChild().getHTMLAttribute("href").trim();
							vurl = vurl.substring(vurl.indexOf("?q=")+3, vurl.indexOf("&fr="));
							v[2] = vurl;								
						}else{
							continue;
						}
						if(complicated){
							break;
						}
					}					
					if(complicated){
						//do not add this vendor info, as more will be added from another crawler
						this.vendors.addAll(this.getResultTable(cont));
					}else{
						//add
						//System.out.println(v[1]);
						for(int i =2 ; i<v.length; i++){
							if(v[i] == null){
								v[i] = Builder.NULL;
							}
						}
						if(v[0]!=null && v[1]!=null){
							this.vendors.add(v);
						}
					}
					
				}			
				break; //don't continue with other tables, just this one is sufficient;
			}
			
		}
	}
	//end of inner class
	
}
